<template>
  <div class="login" @keyup.enter='login'>
  <el-form status-icon :rules='rules' ref="form" :model="form" label-width="80px">
    <img src="../assets/avatar.jpg" alt="">
    <el-form-item label="用户名"  prop='username'>
      <el-input v-model="form.username" placeholder='请输入用户名'></el-input>
    </el-form-item>
    <el-form-item label="密码" prop='password' >
      <el-input type='password' v-model="form.password" placeholder='请输入密码'></el-input>
    </el-form-item>
    <el-form-item>
      <el-button type="primary" @click="login" class="loginBtn">登录</el-button>
      <el-button type="danger" @click="reset">重置</el-button>
    </el-form-item>
</el-form>
  </div>
</template>

<script>
export default {
  data () {
    return {
      form: {
        username: '',
        password: ''
      },
      rules: {
        username: [
          { required: true, message: '请输人用户名', trigger: ['change', 'blur'] },
          { min: 3, max: 9, message: '长度在 3 到 9 个字符', trigger: ['change', 'blur'] }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: ['change', 'blur'] },
          { min: 6, max: 12, message: '长度在 6 到 12 个字符', trigger: ['change', 'blur'] }
        ]
      }
    }
  },
  methods: {
    login () {
      this.$refs.form.validate(valid => {
        if (!valid) return
        this.$axios.post('login', this.form).then(res => {
          const { meta, data } = res
          if (meta.status === 200) {
            this.$message({
              message: meta.msg,
              duration: 2000,
              type: 'success',
              center: true
            })
            localStorage.setItem('token', data.token)
            this.$router.push('/index')
          } else {
            this.$message({
              message: meta.msg,
              duration: 2000,
              type: 'error',
              center: true
            })
          }
        })
      })
    },
    reset () {
      this.$refs.form.resetFields()
    }
  }
}
</script>

<style lang = 'scss' scoped>
  .login{
    width: 100%;
    height: 100%;
    background-color: hotpink;
    overflow: hidden;
    .el-form{
      padding: 20px;
      width: 400px;
      padding-top: 70px;
      background-color: #fff;
      border-radius: 20px;
      margin: 0 auto;
      margin-top: 200px;
      position: relative;
      img{
      position: absolute;
      border-radius: 50%;
      left: 50%;
      transform: translateX(-50%);
      top:-70px;
      border: 5px solid #fff;
      }
     .loginBtn{
        margin-right: 70px;
      }
    }
  }
</style>
